package com.xspace.android.xafcommon.db.dbsub;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

//import com.umeng.analytics.MobclickAgent;
import com.xspace.android.imcommon.utils.AppUtil;
import com.xspace.android.xafcommon.been.BottomViewInfo;
import com.xspace.android.xafcommon.db.BaseSql;

public class TabHostDBBiz extends BaseSql {
	private String tabPachage = "tabPachage";
	private String tabName = "tabName";
	private String tabImg = "tabImg";
	private String tabImgSelected = "tabImgSelected";
	private String tabID = "tabID";
	private String tabSum = "tabSum";// 浏览次数
	private String tabHostTable = "tabHostTable";

	public TabHostDBBiz() {

	}

	public TabHostDBBiz(MyDBHelper db) {
		super(db);
	}

	@Override
	public void create(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + tabHostTable
				+ " (_ID INTEGER PRIMARY KEY," + tabID + " CHAR(16)," + tabSum
				+ " INTEGER," + tabPachage + " CHAR(32)," + tabName
				+ " CHAR(32)," + tabImg + " BLOB," + tabImgSelected + " BLOB);");
	}

	public void dropTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " +tabHostTable); 
	}
	

	public int saveOrUpdate(BottomViewInfo info) {
		where = tabID + "='" + info.tabID + "'";
		values = new ContentValues();
		values.put(tabID, info.tabID);
		values.put(tabPachage, info.packageName);
		values.put(tabName, info.title);
		values.put(tabImg, AppUtil.Bitmap2Bytes(info.unselectedId));
		values.put(tabImgSelected, AppUtil.Bitmap2Bytes(info.selectedId));
		return super.saveOrUpdate(tabHostTable);
	}

	public ArrayList<BottomViewInfo> query() {
		ArrayList<BottomViewInfo> list = new ArrayList<BottomViewInfo>();
		try {
			where = null;
			Cursor cur = db.query(tabHostTable, where);
			if (cur != null) {
				if (cur.getCount() != 0) {
					cur.moveToFirst();
					int length = cur.getCount();
					for (int i = 0; i < length; i++) {
//						BottomViewInfo info = new BottomViewInfo(
//								cur.getString(cur.getColumnIndex(tabPachage)),
//								cur.getString(cur.getColumnIndex(tabName)),
//								Tools.createImage(cur.getBlob(cur
//										.getColumnIndex(tabImgSelected))),
//								Tools.createImage(cur.getBlob(cur
//										.getColumnIndex(tabImg))),
//								cur.getString(cur.getColumnIndex(tabID)),
//								cur.getInt(cur.getColumnIndex(tabSum)));
//						list.add(info);
//						cur.moveToNext();
					}
					cur.close();
				}
			}
		} catch (Exception e) {
//			MobclickAgent.reportError(MyApplication.mContext, e);
			e.printStackTrace();
		}
		finally{
			db.close();
		}
		return list;
	}

	public int delete(String id) {
		selection = tabID + " =?";
		selectionArgs = new String[] { id };
		return super.delete(tabHostTable);
	}


	@Override
	public void upgrade(SQLiteDatabase arg0) {
		
	}



}
